package com.jz.community.moduleshopping.cardGoods.task;

import android.app.Activity;

import com.jz.community.basecomm.bean.RegionsBean;
import com.jz.community.basecomm.constant.Constant;
import com.jz.community.basecomm.net.OkHttpUtil;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.task.RxTask;
import com.jz.community.basecomm.utils.BaseSpUtils;
import com.jz.community.basecomm.utils.JsonUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.commview.view.widget.ProgressDialogManager;
import com.jz.community.moduleshopping.cardGoods.info.CardBrandInfo;
import com.jz.community.moduleshopping.cardGoods.info.CardHomeInfo;

import java.util.HashMap;

/**
 * Created by Tony.Fan on 2018/7/03 09:16
 */
public class GetCardBrandTask extends RxTask<String, Integer, CardBrandInfo> {

    private Activity activity;
    private ITaskCallbackListener taskListener;

    public GetCardBrandTask(Activity activity,
                            ITaskCallbackListener taskListener) {
        super(activity);
        this.activity = activity;
        this.taskListener = taskListener;
    }

    @Override
    protected CardBrandInfo doInBackground(String... params) {
        // 执行请求
        String result = OkHttpUtil.get(Constant.CARD_BRAND_URL,getParam());
        CardBrandInfo info = null;
        if (!Preconditions.isNullOrEmpty(result)) {
            info = JsonUtils.parseObject(result, CardBrandInfo.class);
        }
        return info;
    }

    @Override
    protected void onPostExecute(CardBrandInfo result) {
        // 执行结果回调函数
        taskListener.doTaskComplete(result);
        ProgressDialogManager.dismissProgressDialog();
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        ProgressDialogManager.showDialog(activity);
        super.onPreExecute();
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }


    /**
     * 获取请求所需的参数
     *
     * @param
     * @return
     */
    private HashMap<String, Object> getParam() {
        HashMap<String, Object> param = new HashMap<>();
        RegionsBean.EmbeddedBean.ContentBean region = BaseSpUtils.getInstance().getRegion(activity);
        param.put("cityCode", region.getCode());
        param.put("page", 0);
        param.put("size", 4);
        return param;
    }
}